package com.jkl.service.drIndex;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jkl.comm.StringUtils.DataUtils;
import com.jkl.comm.StringUtils.StringUtils;
import com.jkl.dao.drIndex.DrIndexMapper;
import com.jkl.dao.drIndexFormula.DrIndexFormulaMapper;
import com.jkl.entity.drIndex.DrIndex;
import com.jkl.entity.drIndexFormula.DrIndexFormula;
import com.jkl.entity.param.BSParam;
import com.jkl.service.drIndex.inter.DrIndexServiceInterface;
import com.jkl.service.param.inter.IBSParamService;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
@Service
@Slf4j
public class DrIndexService implements DrIndexServiceInterface {
	
	@Autowired
	private DrIndexMapper DrIndexMapper;
	@Autowired
	private DrIndexFormulaMapper DrIndexFormulaMapper;
	
	@Autowired
	private IBSParamService paramService;
	
	//查询全部指标
	@Override
	public List<DrIndex> selectParamById(DrIndex drIndex) {
		Example example1 = new Example(DrIndex.class);
		example1.createCriteria().andEqualTo("rowId", drIndex.getRowId());
		Example example = new Example(DrIndex.class);
		List<DrIndex> dept = DrIndexMapper.selectByExample(example1);
		//List<DrIndex> List = DrIndexMapper.selectParamById(drIndex);

		return DrIndexMapper.selectByExample(example);
	}
	//指标表和公式表关联查询
	@Override
	public PageInfo<DrIndex> selectjoinId(DrIndex drIndex) {
		PageHelper.startPage(drIndex.getCurrentPage(), drIndex.getPageSize());
		String operTime=DataUtils.getDayStart();
		drIndex.setOperTime(operTime);
		List<DrIndex> drIndexList = DrIndexMapper.selectjoinId(drIndex);
		if(drIndexList.size()!=0) {
			for (int i = 0; i < drIndexList.size(); i++) {
				//level link cycle
				BSParam bSParam1=new BSParam();
				bSParam1.setGroupCode("level");
				
				if(drIndexList.get(i).getLevel()!=null) {
					bSParam1.setParamCode(drIndexList.get(i).getLevel());
					BSParam bSParamTemp1 = paramService.queryCode(bSParam1);
					if(bSParamTemp1!=null) {
						drIndexList.get(i).setLevel(bSParamTemp1.getValueCode());
					}
				}
				
				BSParam bSParam2=new BSParam();
				bSParam2.setGroupCode("link");
				
				if(drIndexList.get(i).getLink()!=null) {
					bSParam2.setParamCode(drIndexList.get(i).getLink());
					BSParam bSParamTemp2 = paramService.queryCode(bSParam2);
					if(bSParamTemp2!=null) {
						drIndexList.get(i).setLink(bSParamTemp2.getValueCode());
					}
				}
				
				BSParam bSParam3=new BSParam();
				bSParam3.setGroupCode("cycle");
				
				if(drIndexList.get(i).getCycle()!=null) {
					bSParam3.setParamCode(drIndexList.get(i).getCycle());
					BSParam bSParamTemp3 = paramService.queryCode(bSParam3);
					if(bSParamTemp3!=null) {
						drIndexList.get(i).setCycle(bSParamTemp3.getValueCode());
					}
				}
				
				
			}
		}
		
		//PageInfo<DrIndex> pageInfo = new PageInfo<>(drIndexList);
		return new PageInfo<>(drIndexList);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public int saveJoin(DrIndex drIndex) {
		drIndex.setStatus("1");
		//自增序列 sequenceId
		int sum= DrIndexMapper.sequenceId();
		Integer i=0;
		if(sum!=0) {
			String sun = StringUtils.changeNumberforString(Integer.valueOf(sum).longValue(),4);
			// 指标编码
			String indexCode=drIndex.getLevel()+drIndex.getLink()+drIndex.getCycle()+drIndex.getType()+sun;
			drIndex.setIndexCode(indexCode);
			String s=DataUtils.getDayStart();
			drIndex.setOperTime(s);
			drIndex.setSource("2");
			i = DrIndexMapper.insert(drIndex);
			if(i==1) {
				DrIndexFormula drIndexFormula=new DrIndexFormula();
				drIndexFormula.setIndexCode(drIndex.getRowId());
				drIndexFormula.setFormulaNote(drIndex.getDrIndexFormula().getFormulaNote());
				drIndexFormula.setIndexFormula(drIndex.getDrIndexFormula().getIndexFormula());
				DrIndexFormulaMapper.insert(drIndexFormula);
			}	
			
		}
		
		return i;
	}

	@Override
	public int updateDrindex(DrIndex drIndex) {
		int i = DrIndexMapper.updateByPrimaryKey(drIndex);
		if(i!=0) {
		    if(org.apache.commons.lang3.StringUtils.equalsIgnoreCase(drIndex.getType(),"m")){
				DrIndexFormula tempDrIndexFormula = drIndex.getDrIndexFormula();
				tempDrIndexFormula.setIndexCode(drIndex.getRowId());
				DrIndexFormulaMapper.updateByPrimaryKey(tempDrIndexFormula);
			}
			return i;
		}else {
			return 0;
		}
	}
}
